<%--
  Created by IntelliJ IDEA.
  User: 86183
  Date: 2022/5/8
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <link rel="shortcut icon" href="#"/>
    <title>设置店铺</title>
    <style>
        body{
            color: white;
            background-color: black;
        }
        span{
            width: 200px;
            height: 5px;
            color: red;
            font-size: 4px;
        }
    </style>
</head>
<body>
    <form method="post" action="/FoodServlet" enctype="multipart/form-data">
        <input type="hidden" name="action" value="updateShop">
        <input type="hidden" name="shopId" id="shopId" value="">
        店名：<input type="text" id="name" name="name" value="" maxlength="10"><br>
        <span id="checkName"></span>
        <br>
        介绍：<textarea id="introduction" name="introduction" maxlength="255"></textarea><br>
        <span id="checkIntroduction"></span>
        <br>
        资产: <input type="number" id="money" name="money" max="9999999999" min="0" step="0.01"/> 元
        <br>
        <label for="pictureUrl">店铺图片:</label>
        <img border="0" src=""  id="picture" alt="picture" width="200" height="200">
        <input type="file" id="pictureUrl" name="pictureUrl"><br>
        <br>
        请选择您的店铺地址: 地区:<select id="district" name="district"></select>
        街区:<select id="street" name="street" style="margin-left: 10px"></select>
        详细地址:<input type="text" id="detailAddress" name="detailAddress" maxlength="255">
        <br>
        <span id="checkAddress"></span>
        <br>
        <input type="submit">
    </form>
    <button onclick="goBack()">返回</button>
</body>
<script>
    let params = new URLSearchParams(location.search);
    let name=params.get("name");
    let shopId=params.get("id");
    let introduction=params.get("introduction");
    let money=params.get('money');
    let district=params.get("districtName");
    let street=params.get("streetName");
    let detailedAddress=params.get("detailedAddress");
    let picture=params.get("picture")
    document.getElementById("shopId").value=shopId;
    document.getElementById("name").value=name;
    document.getElementById("introduction").value=introduction;
    document.getElementById("detailAddress").value=detailedAddress;
    document.getElementById("money").value=money;
    let src="http://localhost/picture/upload/"+picture;
    document.getElementById("picture").src=src;

    let state = params.get('state');
    if(state!=null){
        console.log(state);
        if(state=="SUCCESS"){
            alert("添加成功!")
            window.location.href="Seller.jsp";
        }else{
            console.log("fail")
            alert("操作失败,请稍后再试")
        }
    }

    function getCookie(name) {
        if (document.cookie.length > 0) {
            let start = document.cookie.indexOf(name + "=");
            if (start != -1) {
                start = start + name.length + 1;
                let end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                return decodeURI(document.cookie.substring(start, end));
            }
        }
        return null;
    }
    function goBack(){
        window.location="Seller.jsp";
    }
    document.getElementById("money").onblur=function (){
        let newMoney=parseFloat(document.getElementById("money").value);
        console.log(newMoney);
        console.log(parseFloat(money));
        if(newMoney<money){
            console.log("您的资产不能充值为较小值!!")
            alert("您的资产不能充值为较小值!!")
            document.getElementById("money").value=money;
        }
    }
    window.onload=function (){
        function addDistrict(){
            let json={
                "district" : null,
                "mode": "district"
            }
            let xhr=new XMLHttpRequest();
            xhr.open("post","http://localhost:80/updateHomePage",true);
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
            xhr.responseType='json';
            xhr.send("json="+JSON.stringify(json));
            xhr.onreadystatechange=function(){
                if(xhr.status==200 && xhr.readyState==4){
                    let data=xhr.response;
                    console.log(data);
                    if(data["code"]==404){
                        console.log(data.state)
                    }else{
                        let districts=JSON.parse(data.data);
                        console.log(districts)
                        for(let i=0;i<districts.length;i++){
                            document.getElementById("district").options.add(
                                new Option(districts[i]["name"],districts[i]["name"]));
                            console.log(document.getElementById("district"));
                            if(districts[i]["name"]==district){
                                document.getElementById("district").options[districts[i]["name"]].selected = true;
                            }
                        }
                    }
                }
            }

        }
        function addStreet(){
            document.getElementById("district").onchange=function (){
                let obj=document.getElementById("district");
                let district = obj.options[obj.selectedIndex].text;
                console.log(district);
                let json={
                    "district":district,
                    "mode":"street"
                }
                let xhr=new XMLHttpRequest();
                xhr.open("post","http://localhost:80/updateHomePage",true);
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
                xhr.responseType='json';
                xhr.send("json="+JSON.stringify(json));
                xhr.onreadystatechange=function(){
                    if(xhr.status==200 && xhr.readyState==4){
                        let data=xhr.response;
                        console.log(data);
                        if(data["code"]==404){
                            console.log(data.state)
                        }else{
                            let streets=JSON.parse(data.data);
                            console.log(streets)
                            let obj=document.getElementById("street");
                            obj.options.length=0;
                            for(let i=0;i<streets.length;i++){
                                obj.options.add(new Option(streets[i]["name"],streets[i]["name"]));
                            }
                        }
                    }
                }
            }
        }
        addDistrict();
        addStreet();
    }
    document.getElementById("name").onblur = function (){
        let userName = document.getElementById("name").value;
        if(userName.trim() == ""){
            return;
        }else{
            let xhr=new XMLHttpRequest();
            xhr.open("post","http://localhost:80/checkShopName",true);
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.responseType='json';
            xhr.send("shopName="+JSON.stringify(userName));
            xhr.onreadystatechange=function(){
                if(xhr.status==200 && xhr.readyState==4){
                    let data = xhr.response;
                    console.log(data);
                    if(data["code"]==200){
                        document.getElementById("checkName").innerText=data.state;
                    }
                }
            }
        }
    }
    document.getElementById("name").onfocus=function(){
        document.getElementById("checkName").innerHTML='';
    }
    document.getElementById("introduction").onblur=function (){
        let name = document.getElementById("introduction").value;
        if(name.trim() == ""){
            return;
        }else{
            let xhr=new XMLHttpRequest();
            xhr.open("post","http://localhost:80/sensitiveCheck",true);
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.responseType='json';
            xhr.send("content="+JSON.stringify(name));
            xhr.onreadystatechange=function(){
                if(xhr.status==200 && xhr.readyState==4){
                    let data = xhr.response;
                    console.log(data);
                    if(data["code"]==404){
                        document.getElementById("checkIntroduction").innerHTML=data.state;
                    }
                }
            }
        }
    }
    document.getElementById("introduction").onfocus=function (){
        document.getElementById("checkIntroduction").innerHTML='';
    }
    //
    // document.getElementById("over").onclick=function () {
    //     let newName = document.getElementById("name").value;
    //     console.log(name);
    //     let introduction = document.getElementById('introduction').value;
    //     console.log(introduction);
    //     let district = document.getElementById("district");
    //     let newDistrict = district.options[district.selectedIndex].text;
    //     console.log(newDistrict)
    //     let street = document.getElementById("street");
    //     let newStreet = district.options[street.selectedIndex].text;
    //     console.log(newStreet)
    //     let address = document.getElementById("detailAddress").value;
    //     console.log(address);
    //     let money = document.getElementById("money").value;
    //     console.log(money);
    //
    //     document.getElementById("money").blur();
    //
    //     let checkName = document.getElementById("checkName").innerText;
    //     let checkIntroduction = document.getElementById("checkIntroduction").innerText;
    //     if (checkName == '' && checkIntroduction == '') {
    //         let json = {
    //             "ownerId": getCookie("id"),
    //             "mode": "updateShop",
    //             "shopId": shopId,
    //             "name": newName,
    //             "introduction": introduction,
    //             "newDistrict": newDistrict,
    //             "newStreet": newStreet,
    //             "address": address,
    //             "money": money
    //         }
    //         let xhr = new XMLHttpRequest();
    //         xhr.open("post", "http://localhost:80/intoShop", true);
    //         xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    //         console.log(json)
    //         xhr.send("json=" + JSON.stringify(json));
    //         console.log("before:json=" + JSON.stringify(json));
    //         xhr.onreadystatechange = function () {
    //             if (xhr.status == 200 && xhr.readyState == 4) {
    //                 let data = xhr.response;
    //                 console.log(data)
    //                 let json = JSON.parse(data)
    //                 console.log(json)
    //                 console.log(json.code)
    //                 if (json.code == 200 && json.state == 'SUCCESS') {
    //                     alert(json.state)
    //                 } else if (json.code == 404) {
    //                     console.log(json.data)
    //                 }
    //             }
    //         }
    //     }
    //
    // }
</script>
</html>
